var app = getApp();
var util = require('../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    movie: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var movieId = options.id;
    var url = app.globalData.doubanBase + "/v2/movie/subject/" + movieId;
    util.http(url, this.processDoubanData);
  },

  onReady: function(options) {
    this.videoContext = wx.createVideoContext("trailerVideo");
  },

  processDoubanData: function(data) {
    var director = {
      avatar: "",
      name: "",
      id: ""
    };
    if (data.directors[0] != null) {
      if (data.directors[0].avatars != null) {
        director.avatar = data.directors[0].avatars.large;
      }
      director.name = data.directors[0].name;
      director.id = data.directors[0].id;
    }
    var trailers = [/*{
      url: "http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400",
      type: "0"
    }*/]
    for (var i = 0; i < data.photos.length; i++) {
      trailers.push({
        url: data.photos[i].thumb,
        type: '1'
      });
    }
    var movie = {
      movieImg: data.images ? data.images.large : "",
      country: data.countries[0],
      title: data.title,
      originalTitle: data.original_title,
      wishCount: data.wish_count,
      commentCount: data.comments_count,
      year: data.year,
      genres: data.genres.join("、"),
      stars: util.convertToStarsArray(data.rating.stars),
      score: data.rating.average,
      director: director,
      casts: util.convertToCastString(data.casts),
      castsInfo: util.convertToCastInfos(data.casts),
      summary: data.summary,
      trailers: trailers //data.trailer_urls
    }
    this.setData({
      movie: movie
    });
  },

  viewMoviePostImg: function(event) {
    var src = event.currentTarget.dataset.src;
    wx.previewImage({
      current: src,
      urls: [src],
    })
  },

  videoPlay: function(event) {
    this.videoContext.requestFullScreen();
  },

  fullScreenChange: function(event) {
    if (!event.detail.fullScreen) {
      this.videoContext.pause();
    }
  },

  onActorTap: function(event) {
    var celebrityId = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../celebrities/celebrity?id=' + celebrityId,
    })
  }

})